package pers.amos.edu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.amos.edu.common.GlobalResult;
import pers.amos.edu.common.RedisKeyConstant;
import pers.amos.edu.exception.EducationException;
import pers.amos.edu.exception.ExceptionEnum;
import pers.amos.edu.mapper.CollectionSchoolMapper;
import pers.amos.edu.mapper.SchoolMapper;
import pers.amos.edu.mapper.UserMapper;
import pers.amos.edu.pojo.CollectionSchool;
import pers.amos.edu.pojo.School;
import pers.amos.edu.pojo.User;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author amos wong
 * @create 2020-02-02 15:01
 */

@Service
public class CollectionSchoolService {

    @Autowired
    private CollectionSchoolMapper collectionSchoolMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional
    public GlobalResult addCollection(String openId, Integer schoolId) {
        Boolean isMember = redisTemplate.opsForSet().isMember(RedisKeyConstant.USER_COLLECT_SCHOOL_KEY + openId, schoolId);
        if (isMember) {
            return new GlobalResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "请勿重复收藏", null);
        }
        Integer count = (Integer) redisTemplate.opsForValue().get(RedisKeyConstant.USER_COLLECT_SCHOOL_COUNT + openId);
        if (count == null) {
            redisTemplate.opsForValue().set(RedisKeyConstant.USER_COLLECT_SCHOOL_COUNT + openId, 0);
            count = 0;
        }
        if (count >= 30) {
            return new GlobalResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "收藏的学校个数已达上限30个", null);
        }
        try {
            School school = schoolMapper.selectByPrimaryKey(schoolId);
            User user = userMapper.selectByPrimaryKey(openId);
            if (school == null) {
                return new GlobalResult(HttpStatus.NOT_FOUND.value(), "收藏失败,未找到相应学校", null);
            }
            CollectionSchool collectionSchool = new CollectionSchool();
            collectionSchool.setSchoolId(schoolId);
            collectionSchool.setSchoolName(school.getName());
            collectionSchool.setOpenId(user.getOpenId());
            collectionSchool.setNickName(user.getNickName());
            collectionSchool.setCreateTime(new Date());
            collectionSchoolMapper.insert(collectionSchool);
            redisTemplate.opsForSet().add(RedisKeyConstant.USER_COLLECT_SCHOOL_KEY + openId, schoolId);
            redisTemplate.opsForValue().increment(RedisKeyConstant.USER_COLLECT_SCHOOL_COUNT + openId);
        } catch (Exception e) {
            e.printStackTrace();
            return new GlobalResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "收藏失败", null);
        }
        return new GlobalResult(HttpStatus.OK.value(), "收藏成功", null);
    }

    public GlobalResult cancelCollection(String openId, Integer schoolId) {
        Boolean isMember = redisTemplate.opsForSet().isMember(RedisKeyConstant.USER_COLLECT_SCHOOL_KEY + openId, schoolId);
        if (!isMember) {
            throw new EducationException(ExceptionEnum.NOT_FOUND);
        }
        try {
            Example example = new Example(CollectionSchool.class);
            example.createCriteria().andEqualTo("schoolId", schoolId).andEqualTo("openId", openId);
            collectionSchoolMapper.deleteByExample(example);
            redisTemplate.opsForSet().remove(RedisKeyConstant.USER_COLLECT_SCHOOL_KEY + openId, schoolId);
            redisTemplate.opsForValue().decrement(RedisKeyConstant.USER_COLLECT_SCHOOL_COUNT + openId);
        } catch (Exception e) {
            e.printStackTrace();
            return new GlobalResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "取消收藏失败", null);
        }
        return new GlobalResult(HttpStatus.OK.value(), "取消收藏成功", null);
    }

    public List<CollectionSchool> getSchoolCollection(String openId) {
        Example example = new Example(CollectionSchool.class);
        example.createCriteria().andEqualTo("openId", openId);
        List<CollectionSchool> collectionSchoolList = collectionSchoolMapper.selectByExample(example);
        return collectionSchoolList;
    }
}
